package henry.flink.demo;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.List;

public class FlatMapDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //使用fromElements source生成数据

        DataStreamSource<List<String>> lines = env.fromElements(DataSource.getPersonJsonList());
        DataStream<String> words = lines.flatMap(new FlatMapFunction<List<String>, String>() {
            @Override
            public void flatMap(List<String> line, Collector<String> out) throws Exception {
                StringBuilder sb = new StringBuilder();
                for (String str : line) {
                    sb.append(str);
                }
                System.out.println(sb);
                out.collect(sb.toString());
            }
        });
        //打印
        words.print();
        env.execute("FlatMapDemo");
    }
}